<?PHP
date_default_timezone_set("Asia/Chongqing");
require_once('public/cache.inc.php');
$cachedir = "../cache/";
if (isset($il_cachedir))	$cachedir = $il_cachedir;
$cachedir .= date('Y-m-j')."/";
$cache = new Cache($cachedir,600);	// 10分钟
if (isset($_GET["refresh"]))
{
	$url = str_replace("&refresh","",$cache->geturl());
	$cache->clean2($cache->getcacheid2($url));
}

$hitCache = $cache->load();
if (!isset($hitCache) || !$hitCache) {
?>
<?PHP 
if (!isset($il_col)) $il_col = 4;
if (!isset($il_order)) $il_order = "h";
if (!isset($il_param)) $il_param = "";
if (!isset($il_catelog)) $il_catelog = "";
if (!isset($il_state)) $il_state = 2;

if (!isset($_SESSION["_rnd_order_seed_"]))	$_SESSION["_rnd_order_seed_"]=mt_rand();
$ilRndOrderSeed=$_SESSION["_rnd_order_seed_"];

// 搜索条件
if (!isset($il_userid)) $il_userid = "";
if (!isset($il_tag)) $il_tag = "";

$nPage = CheckUrlParamNoDie("page"); //当前页数
if ($nPage == "") $nPage = 1;

//
$sql = "from goods_items where state=$il_state and pic_url<>''";
if ($il_userid != "")
	$sql .= " and user_id = $il_userid";

//catelog
if($il_catelog!="")
{
	$sql.=" and sys_tag='$cl_catelog'";
}

// tag
//$il_tag ="";
if ($il_tag != "")
{
	$i=0;
	$vecTag = explode(" ", trim($il_tag));
	$sql .= " and (tag like '%$vecTag[$i]%'";
	for($i=1; $i < count($vecTag); ++$i)
		$sql .= " or tag like '%$vecTag[$i]%'";
	
	$sql .= ")";
}

// new
//echo $il_order;
$scope = "";
if (isset($il_IsSearch) && $il_IsSearch)
	;
else if ($il_order == "h")
//	$scope .= " and datediff(now(), time)<=3";	// 24小时，实际取的是前3天的
	$scope .= " and (favorite >= 2 or hits >= 100)";
else if($il_order == "w")
	$scope .= " and datediff(now(), time)<=7";
else if($il_order == "m")
	$scope .= " and datediff(now(), time)<=30";
//else										// 最新分享，实际取的是前3天的
//	$scope .= " and (datediff(now(), time)<=3 or datediff(now(), time)>20)";

// 取数量
$row = mysql_fetch_assoc(mysql_query("select count(*) as tmp ".$sql.$scope));
$allCount = $row["tmp"];
//echo $allCount;
if ($allCount < 30)
{
//	$scope = " and datediff(now(), time)<=30";	// 如果数量不够，改成30天的
	$scope = "";	// 如果数量不够，不限制
	$row = mysql_fetch_assoc(mysql_query("select count(*) as tmp ".$sql.$scope));
	$allCount = $row["tmp"];
}
$sql .= $scope;


// order
if ($il_order == "n")	// 24小时和最新分享，都取前3天的，随机，24小时是直接把商品实际分享时间进行折射，最新分享是把时间重排
//	$sql .= "  order by rand(now())";
	$sql .= "  order by time desc";
else
	$sql .= " order by rand($ilRndOrderSeed)";

$sql = "select id, num_iid, pic_url, url, item_title, time, fee, commission, favorite, r_desc ".$sql;
$PAGE_SIZE = 100;
$limit = " limit ". ($nPage - 1)*$PAGE_SIZE .",$PAGE_SIZE";
$sql .= $limit;
//echo $sql;
//exit;
$result = mysql_query( $sql );
$i=0;
$nMaxCount = mysql_num_rows($result);
//echo floor($allCount / $PAGE_SIZE);
// echo $nPage; 
?>

<div>
	<!--div style=" padding:0;" class="container_12"></div-->
            <!-----推荐内容------->
            <div id="wall" class="itemDetail">
            <?PHP 
			if($allCount == 0){
			?>
            <div class="SearchFailed">
            	<div class="failed">
                    <div class="fai">抱歉，没有找到和<b style='color: red;'><?php if(isset($tag)) echo $tag; ?></b>相关的商品</div>
                    <div class="sug">
                        <ul>
                            <h2>兔丝街建议：</h2>
                            <li>请查看输入的文字是否有误？</li>
                            <li>不要输入太长的句子，尽量使用简短的词。</li>
                            <li>或去<a href='http://www.tusijie.com/kk/' style="color:red;">看看兔丝们在买些什么</a>，看看能否找到您喜欢的商品。</li>
                        </ul>
                    </div>
                </div>
                <!----猜你喜欢的其他宝贝--->
                <div class="grid_3 omega Other">
                    <h2>兔达人的其它分享</h2>
                    <ul class="guess">
                    <?PHP
                    if ($g_userid!="")
                        $sql = "select * from goods_items where state=$il_state and id <> $id and user_id=$g_userid order by time desc,hits desc limit 6";
                    else
                        $sql = "select * from goods_items where state=$il_state order by time desc,hits desc limit 8";
    
                    $res2 = mysql_query( $sql );
                    while( $res2 && $row = mysql_fetch_assoc( $res2 ))
                    {
                        $tmpTag = explode(" ", trim($row["tag"]));
                        $tmpTag = $tmpTag[0];
                        echo "<li><a target='_blank' href='../item/?id=".$row["id"]."'><img src='".$row["pic_url"]."_90x90.jpg"."' title='' alt=''></a><span><a target='_blank' href='../tag?tag=".urlencode($tmpTag)."'>$tmpTag</a></span></li>";
                    }
                    ?>
                    </ul>
                    <div class="clear"></div>
                </div>
            </div>
                
			<?php } ?>
			
            <?PHP
			$tS = time();
			for ($k = 0; $k < $il_col; ++$k) {
			?>
            	<div class="column grid_3 <?PHP if ($k == 0) echo "alpha"; else if ($k == $il_col-1) echo "omega";?>">
	                <?PHP 
						for( $i=$k; $i < $nMaxCount; $i += $il_col)
						{
					?>
               	  <div class="brand_new">
                  		<p class="images"><a target="_blank" href="../item/?id=<?PHP echo mysql_result($result, $i, "id"); ?>" class="first" title="<?PHP echo mysql_result($result, $i, "item_title"); ?>"><img width="180" src="<?PHP echo mysql_result($result, $i, "pic_url")."_250x250.jpg"; ?>" onerror="this.src='<?PHP echo mysql_result($result, $i, "pic_url"); ?>'" alt="<?PHP echo mysql_result($result, $i, "item_title"); ?>" /></a><span class="jg"><span><b>¥</b><?PHP echo mysql_result($result, $i, "fee"); ?></span><i></i></span></p>
                        <p class="comm"><span class="xi"  style="float:left;"><a style="float:left;" onclick="onfavorite(<?PHP echo mysql_result($result, $i, "id"); ?>)">我喜欢</a><b class="nums" id="favorite_<?PHP echo mysql_result($result, $i, "id"); ?>" style="float:left; <?php if (mysql_result($result, $i, "favorite") == 0) echo "display:none;"; ?> "><?PHP echo mysql_result($result, $i, "favorite"); ?></b></span><span class="time" style="float:right; padding:2px 0 0; background:none;"><?PHP
                        $item_time = strtotime(mysql_result($result, $i, "time"), time());
						/*if ($il_order == "h")	// 24小时最热，要对3天内的商品作时间映射
						{
							$t = strtotime(date("Y-m-j"), time());
							$span = time() - $t;
							$item_time %= $span;
							$item_time += $t;
						}
						else if ($il_order == "n")	// 最新分享排序，实际是取6天内的随机商品，然后显示时间，缓存用10分钟							
							$item_time = $tS - mt_rand(0,1)*60 - $k*60 - ($i-$k)*60;*/
						
						$minTime = (time() - $item_time) / 60;
						//$hourTime = (time() - $item_time) / 3600;
						if ($minTime < 60)
							echo intval(max($minTime,1)).' 分钟前';
						//else if ($hourTime < 6)
						//	echo intval($hourTime).' 小时前';
						else if ( date('mj', $item_time) == date('mj', time()))
							echo "今天 ".date('G:i', $item_time);
						else if ( date('mj', $item_time) == date('mj', time())-1 )
							echo "昨天 ".date('G:i', $item_time);
						else
							echo date('m月j日 G:i', $item_time);
//						echo mysql_result($result, $i, "time");
						
						?></span></p>
                		
           			    <p class="price Recommend" <?php if(mysql_result($result, $i, "r_desc") == "") echo 'style="display:none"'; ?>>推荐说：<span style="color:#666;"><?PHP echo ReplaceBiaoQing(mysql_result($result, $i, "r_desc")); ?></span></p>
                  </div>
                    <?PHP 
					}
					?>
              </div>
              <?PHP } ?>
              
              <?PHP if ($allCount > $PAGE_SIZE) { ?>
              <!----page--start-->
              <div style="" class="grid_12 alpha omega pager">
                	<div id="pageNav">                        
                        <?php	
						$all_page = ceil($allCount / $PAGE_SIZE);
						if ($nPage > 1) echo "<a class='pageNext' href='?page=".($nPage-1)."$il_param'>&lt;上一页</a>";
		
						if($all_page<=13){				
							for($i=1;$i<=$allCount / $PAGE_SIZE;$i++){//数字分页
								$Pageshow = ($i!=$nPage)?"<a href='?page=$i$il_param'>".($i)."</a>":"<b>".($i)."</b>";
								echo $Pageshow;
						  	}
						}else{
							for($i=1;$i<=13;$i++){//数字分页
								$Pageshow = ($i!=$nPage)?"<a href='?page=$i$il_param'>".($i)."</a>":"<b>".($i)."</b>";
								echo $Pageshow;
						  	}
						}
						
						if($nPage == 13 || $nPage == ceil($allCount / $PAGE_SIZE)){
							echo "";
						}else if($nPage < 13){
							echo "<a class='pageNext' href='?page=".($nPage+1)."$il_param'>下一页&gt;</a>";
						}
						 ?>
            		</div>
            	</div>
                <!----page--end-->
                <?PHP } ?>
                
     </div>
</div>
<?PHP
$cache->write();
}
?>

<!--下面的函数不能cache-->

<script language="javascript">
function onfavorite(id)
{
<?PHP if (!HasLogin()) { ?>
	var $shadow = $('#modal_shadow');
	var bodyH = $('body').height();
	var winW = $(window).width();
	var l = ($('body').width() - $("#dialog").width()) / 2;
	if($.browser.msie&&($.browser.version == "6.0")&&!$.support.style){
		var t = ($(window).height() - $("#dialog").height()) / 2 + Math.max($('html').scrollTop(), $('body').scrollTop());
	}else{
		var t = ($(window).height() - $("#dialog").height()) / 2;
	}
	$shadow.width(winW);
	$shadow.height(bodyH);
	$("#dialog").css({"display":"block","top":t,"left":l});
	$shadow.css("display","block");
<?PHP } else { ?>

	var url = "http://www.tusijie.com/item/item_static.php?opt=favorite&id=" + id + "&userid=<?PHP echo GetUserId(); ?>";
//	alert(url);
	$.ajax({	url: url,
			async: true,
			success:function(res){
//			alert(res);
			if (res == 1)
			{
				document.getElementById("favorite_"+id).style.display="block";
				nFavorite = document.getElementById("favorite_"+id).innerHTML;
				if (nFavorite == ""){
					nFavorite = 0;
				}else{
					nFavorite = parseInt(nFavorite);
				}
				document.getElementById("favorite_"+id).innerHTML=nFavorite + 1;
			}
			else
				alert("您已经喜欢过了哦~");
           }
		})
<?PHP } ?>
}
</script>
